package com.jike.noobmoney.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.TaskListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.TaskListEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SearchTastListActivityNew extends BaseActivity implements IView {


    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.searchView)
    SearchView searchView;

    private TaskListAdapter adapter;

    private List<TaskListEntity.TaskBean> mData = new ArrayList<>();
    private TaskPresenter taskPresenter;
    private String userid = "";
    private ImageView iv_back;
    public String value;

    @Override
    protected int layoutResID() {
        return R.layout.activity_search_task_list_new;
    }
    @Override
    public boolean ismain() {
        return true;
    }
    @Override
    public Boolean isfits() {
        return true;
    }
    @Override
    protected void initData() {
        taskPresenter = new TaskPresenter(this);
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        String key= getIntent().getStringExtra("key");
//        mData.clear();
//        getData(key);
        searchView.setIconified(false);
        // 设置搜索文本监听
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //当点击搜索按钮时触发该方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                Log.e("","=====query="+query);
                if(query.length()!=0){
                    String value=query.toString();
                    mData.clear();
                    getData(value);
                    searchView.clearFocus();
                }
                return false;
            }

            //当搜索内容改变时触发该方法
            @Override
            public boolean onQueryTextChange(String newText) {
                Log.e("","=====newText="+newText);
                return false;
            }
        });
        TextView txt_search = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
//设置字体大小为14sp
        txt_search.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);//14sp
//设置字体颜色
        txt_search.setTextColor(getResources().getColor(R.color.notice_color));
//设置提示文字颜色
        txt_search.setHintTextColor(getResources().getColor(R.color.hint_color));
        //去掉searchview下划线
        View view_line = searchView.findViewById(android.support.v7.appcompat.R.id.search_plate);
        view_line.setBackgroundColor(Color.TRANSPARENT);
        searchView.setQueryHint("请输入搜索关键字");//设置查询提示字符串
      //  searchView.clearFocus();

        adapter = new TaskListAdapter(context);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);
        //item点击
        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
            TaskListEntity.TaskBean item = (TaskListEntity.TaskBean) adapter.getItem(position);
            Intent intent = new Intent(context, TaskDetailActivity.class);
            intent.putExtra(TaskDetailActivity.t_id, "" + item.getT_id());
            startActivity(intent);
        });



    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;

        }
    }

    /**
     * 获取数据
     */
    private void getData(String value) {
        showProgress();
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        taskPresenter.getSearchTasklist(userid,value, ConstantValue.RequestKey.task_list);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.task_list.equals(actionType)) {
            List<TaskListEntity.TaskBean> task = (List<TaskListEntity.TaskBean>) data;
            if (task != null && task.size() > 0) {
                    mData.clear();
                    mData.addAll(task);
            }
            adapter.refresh(mData);
        }
    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
    }




    private void disLoading() {
        dismissProgress();
    }


}
